package com.lantosec.hmrp.modules.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.kit.tree.TreeNodeVo;
import com.lantosec.hmrp.modules.system.entity.SysArea;
import com.lantosec.hmrp.modules.system.entity.SysDept;
import com.lantosec.hmrp.modules.system.mapper.SysAreaMapper;
import com.lantosec.hmrp.modules.system.service.ISysAreaService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 区域表 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-02-24
 */
@Service
@Primary
@Log4j2
@SuppressWarnings("all")
public class SysAreaServiceImpl extends ServiceImpl<SysAreaMapper, SysArea> implements ISysAreaService {

    @Override
    public List<TreeNodeVo> getAreaTree() {
        return areaTree(null);
    }

    private List<TreeNodeVo> areaTree(String area) {
        List<TreeNodeVo> list = areaList(area);
        List tree = new ArrayList<TreeNodeVo>();
        if (list != null && list.size() > 0) {
            list.forEach(node -> {
                if (!node.isLeaf()) {
                    List<TreeNodeVo> children = areaTree(node.getKey());
                    node.setChildren(children);
                }
                tree.add(node);
            });
        }
        return tree;
    }

    private List<TreeNodeVo> areaList(String area) {
        List<SysArea> list = getAreaById(area);
        if (list != null && list.size() > 0) {
            List<TreeNodeVo> nodes = new ArrayList<TreeNodeVo>();
            list.forEach( result -> {
                nodes.add(new TreeNodeVo( result.getAreaId(),result.getAreaName(),result.getAreaId()));
            });
            return nodes;
        }
        return new ArrayList<TreeNodeVo>();
    }

    private List<SysArea> getAreaById(String area) {
        return baseMapper.getAreaById(area);
    }

    @Override
    public List<TreeNodeVo> getUserAreaTree() {
        return userArea(null);
    }

    private List<TreeNodeVo> userArea(String area) {
        List<TreeNodeVo> list = userInfo(area);
        List tree = new ArrayList<TreeNodeVo>();
        if (list != null && list.size() > 0) {
            list.forEach(node -> {
                if (!node.isLeaf()) {
                    List<TreeNodeVo> children = areaTree(node.getKey());
                    node.setChildren(children);
                }
                tree.add(node);
            });
        }
        return tree;
    }

    private List<TreeNodeVo> userInfo(String area) {
        List<SysArea> list = getAreaById(area);
        if (list != null && list.size() > 0) {
            List<TreeNodeVo> nodes = new ArrayList<TreeNodeVo>();
            list.forEach( result -> {
                //查询当前区域负责人id
                String areaId = result.getAreaId();
                String userId = baseMapper.queryUserIdByAreaId(areaId);
                //查询当前区域负责人姓名
                String userName = baseMapper.queryUserNameByAreaId(areaId);
                nodes.add(new TreeNodeVo( areaId + "," + userId,  result.getAreaName()+","+userName,areaId));
            });
            return nodes;
        }
        return new ArrayList<TreeNodeVo>();
    }

    @Override
    public List<TreeNodeVo> getUserDeptTree() {
        return deptTree(null);
    }

    public List<TreeNodeVo> deptTree(String deptId) {
        List<TreeNodeVo> list = deptNodes(deptId);
        List tree = new ArrayList<TreeNodeVo>();
        if (list != null && list.size() > 0) {
            list.forEach(node -> {
                if (!node.isLeaf()) {
                    List<TreeNodeVo> children = deptTree(node.getKey());
                    node.setChildren(children);
                }
                tree.add(node);
            });
        }
        return tree;
    }

    public List<TreeNodeVo> deptNodes(String deptId) {
        List<SysDept> list = getDeptById(deptId);
        if (list != null && list.size() > 0) {
            List<TreeNodeVo> nodes = new ArrayList<TreeNodeVo>();
            list.forEach(dep -> {
                //查询当前部门盘点负责人id
                String id = dep.getDeptId();
                String userId  = baseMapper.queryUserIdByDeptId(id);
                //查询当前盘点部门负责人姓名
                String userName = baseMapper.queryUserNameByDeptId(id);
                nodes.add(new TreeNodeVo(id + "," + userId,dep.getDeptName() + "," + userName,id));
            });
            return nodes;
        }
        return new ArrayList<TreeNodeVo>();
    }

    public List<SysDept> getDeptById(String id) {
        return baseMapper.getDeptById(id);
    }

    @Override
    public void create(SysArea entity) {
        String parentId = entity.getParentId();
        String areaId = null;
        if(StringUtils.isNotEmpty(parentId)){
            areaId = baseMapper.queryMaxAreaIdByParentId(parentId);
            entity.setAreaId(StringUtils.isEmpty(areaId)? parentId + "001" : areaId);
        }else{
            areaId = baseMapper.queryMaxAreaId();
            entity.setAreaId(StringUtils.isEmpty(areaId)? "001" : areaId);
        }
        baseMapper.insert(entity);
    }

    @Override
    public void updateOrder(String id, Integer areaOrder) {
        SysArea area = new SysArea();
        area.setAreaOrder(areaOrder);
       baseMapper.update(area,new QueryWrapper<SysArea>()
       .eq(String.valueOf(SysArea.ColName.area_id),id));
    }
}
